package com.sea.supply.controller;

import com.codecrane.core.util.DateUtils;
import com.codecrane.core.util.ExcelUtil;
import com.codecrane.core.web.Ajax;
import com.codecrane.core.web.AjaxReturn;
import com.codecrane.core.web.AjaxTableReturn;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sea.product.service.ChannelService;
import com.sea.supply.entity.WareDistribute;
import com.sea.supply.entity.WareProduce;
import com.sea.supply.service.WareDistributeService;
import com.sea.supply.service.WareProduceService;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping(value = "/sea/supply/ware/distribute")
public class WareDistributeController {

    @Autowired
    private WareDistributeService wareDistributeService;
    @Autowired
    private ChannelService channelService;
    /**
     * 管理主页面
     * @param model
     * @return
     */
    @RequestMapping("/main")
    public String main(Model model) {
        //渠道
        model.addAttribute("channel", channelService.findByCnd(null));
        return "/sea/supply/ware/distribute/main";
    }



    /**
     * 分页条件查询
     *
     * @param pagesize
     * @param start
     * @return
     */
    @ResponseBody
    @RequestMapping("/findByCnd")
    public AjaxTableReturn findByCnd(@RequestParam(value = "limit", defaultValue = "10") int pagesize,
                                     @RequestParam(value = "offset", defaultValue = "0") int start,
                                     WareDistribute wareDistribute) {
        start = start > 0 ? start / pagesize + 1 : 1;
        PageHelper.startPage(start, pagesize);
        List<WareDistribute> list = wareDistributeService.findByCnd(wareDistribute);
        PageInfo<WareDistribute> page = new PageInfo<>(list);
        return Ajax.tableDataOk(page.getTotal(), page.getList());
    }

    /**
     * 导出收货数据
     *
     * @param request
     * @param response
     */
    @RequestMapping(value = "/export")
    @ResponseBody
    public void export(HttpServletRequest request, HttpServletResponse response, WareDistribute wareDistribute) {
        List<WareDistribute> list=wareDistributeService.findByCnd(wareDistribute);
        String[] title = {"PO","渠道","SKU","产品名称","颜色","尺码(CM)","尺码(INCH)",
                "国内仓剩余","占有量","仓库"};
        String fileName = "国内仓剩余明细" + DateUtils.dateToString(new Date(),"yyyyMMdd") + ".xls";
        String sheetName = "国内仓剩余明细表";
        String[][] content = new String[list.size()][10];
        for (int i = 0; i < list.size(); i++) {
            content[i] = new String[title.length];
            WareDistribute obj = list.get(i);
            content[i][0] = obj.getProduceNo();
            content[i][1] = obj.getReceiveChannelName();
            content[i][2] = obj.getReceiveSku();
            content[i][3] = obj.getProductName();
            content[i][4] = obj.getProductColorName();
            content[i][5] = obj.getProductCm();
            content[i][6] = obj.getProductInch();
            content[i][7] = obj.getReceiveNumber()+"";
            content[i][8] = obj.getProduceUse()+"";
            content[i][9] = obj.getWareName()+"";
        }
        //创建HSSFWorkbook
        HSSFWorkbook wb = ExcelUtil.getHSSFWorkbook(sheetName, title, content, null);
        //响应到客户端
        try {
            this.setResponseHeader(response, fileName);
            OutputStream os = response.getOutputStream();
            wb.write(os);
            os.flush();
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    //发送响应流方法
    public void setResponseHeader(HttpServletResponse response, String fileName) {
        try {
            try {
                fileName = new String(fileName.getBytes(),"ISO8859-1");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            response.setContentType("application/octet-stream;charset=ISO8859-1");
            response.setHeader("Content-Disposition", "attachment;filename="+ fileName);
            response.addHeader("Pargam", "no-cache");
            response.addHeader("Cache-Control", "no-cache");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

}
